/*
* Copyright (C) Tijs Teulings (tijs AT automatique.nl).
* 
* This software is licensed as per the Creative Commons Attribution License 3.0
* http://creativecommons.org/licenses/by/3.0/
*
* The above copyright notice and this permission notice shall be included in all copies or 
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/*
* DeviceAura: visualizing Roomware bluetooth scanner output
*
* A simple experiment in processing that was fun for the author and shows how a simple
* integration between roomware and processing could work.
*
* version: 0.1 (2008-02-17)
*
* Todo:
*
* - add configuration file for easier use when distributed as binary
* - instead of just removing device from display fade them out or something
* - interaction? 
* - maybe add some movement for a bit more spectacle
* - make starting location and size meaningful based on more data
* - circles are boring...
*
*/

import processing.xml.*;

/* 
* changing the following to true will run the applet fullscreen 
* and without the OS 'window bar'.
*/
boolean full = false;

/*
* set this to the URL of your Roomware Server
*/
String url = "http://localhost:4040";

/*
* the amount of milliseconds after which the roomware
* server is checked for an updated XML file
*/
float refresh = 2000;


/*
* !! Should be no need to edit below this line !!
*/
int widthCanvas;
int heightCanvas;

PFont font;

RoomwareReader reader;
BTDevice devices[];

void setup() {
	
	if (full) {
		widthCanvas = screen.width;
		heightCanvas = screen.height;
	
		// this sets the initial location of you applet (or app)
		frame.setLocation(0,0);
	
	} else {
		// resolution used when not in full screen mode
		widthCanvas = 800; 
		heightCanvas = 600;
	}

	size(widthCanvas, heightCanvas);
	smooth();

	background(0);
	strokeWeight(10); // the width of the 'border' (the second color)
	frameRate(30); // 30 frames/sec seems like a nice speed/performance tradeoff

	// start with an array for 10 devices	
	devices = new BTDevice[10];

	// start scanning
	reader = new RoomwareReader(this, url, refresh);

	// set the display font (SansSerif is built-in so always available)
	font = createFont("SansSerif", 13);

}

void draw() {
	background(0);
	
	// this loops through the device list generated by the reader and draws them
	for (int i=0;i<devices.length;i++) {
		if (devices[i] != null)
			devices[i].draw();
	}
}

public void init() {
	if (full) {
		// getting rid of OS 'decoration' only works when called from init
		frame.setUndecorated(true);
	}
	// call PApplet.init() to take care of business 
	super.init();  
}